package com.sean.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.stream.events.EndDocument;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.cj.xdevapi.JsonArray;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;
import javassist.expr.NewArray;


public class Test2 {
	private static JSONArray geArray;
	
	public static void main(String[] args) {
		String dbName = "eye_bobahappy_co";
		String url = "jdbc:mysql://124.222.136.19:3306/"+dbName;  
        String user = "eye_bobahappy_co";  
        String password = "Zj4DfdPRfnbjT26e";  
        String pref = "eb_";  //表前缀
        String shortName = "company";  //表简写
		
		geArray = new JSONArray();

        JSONArray tables = new JSONArray();
        /*
        1、配置表
        tableName： platform_company_info
        shortName： company
        foreignKey： 下面的外键
        formWay： 0 //表单方式，0表示BuildForm 1表示弹窗
        2、外键
        selfKey： parent_id
        foreClass： platform_company_info
        foreKey： id
        foreName： name
         
        */
        
        //配置表名
//        JSONObject model1 = new JSONObject();
//        model1.put("tableName", "platform_book_cate");
//        model1.put("shortName", "bookcate");
//        model1.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
//	    tables.add(model1);
//	      pro
		/*
        JSONObject model12 = new JSONObject();
		model12.put("tableName", "platform_prohibit_words");
		model12.put("shortName", "petprowords");
		model12.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model12);
	    
		JSONObject model10 = new JSONObject();
		model10.put("tableName", "platform_kinsect_cate");
		model10.put("shortName", "petkinsectcate");
		model10.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model10);
	    

		JSONObject model11 = new JSONObject();
		model11.put("tableName", "platform_sand_cate");
		model11.put("shortName", "petsandcate");
		model11.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model11);
	    

		JSONObject model1 = new JSONObject();
		model1.put("tableName", "platform_vaccinum_cate");
		model1.put("shortName", "petvaccinumcate");
		model1.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model1);
		

		JSONObject model2 = new JSONObject();
		model2.put("tableName", "platform_pet_feed");
		model2.put("shortName", "petfeed");
		model2.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
		
		
        JSONArray foreignKeys1 = new JSONArray();
        JSONObject key = new JSONObject();
        key.put("selfKey", "archive_id");
        key.put("foreClass", "platform_pet_archives");
        key.put("foreKey", "id");
        key.put("foreName", "name");
        foreignKeys1.add(key);
        model2.put("foreignKey", foreignKeys1);
	    tables.add(model2);
		

		JSONObject model3 = new JSONObject();
		model3.put("tableName", "platform_pet_water");
		model3.put("shortName", "petwater");
		model3.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
		
		
        JSONArray foreignKeys2 = new JSONArray();
        JSONObject key2 = new JSONObject();
        key2.put("selfKey", "archive_id");
        key2.put("foreClass", "platform_pet_archives");
        key2.put("foreKey", "id");
        key2.put("foreName", "name");
        foreignKeys2.add(key2);
        model3.put("foreignKey", foreignKeys2);
	    tables.add(model3);

		JSONObject model4 = new JSONObject();
		model4.put("tableName", "platform_pet_sand");
		model4.put("shortName", "petwand");
		model4.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model4);

		JSONObject model5 = new JSONObject();
		model5.put("tableName", "platform_pet_vaccinum");
		model5.put("shortName", "petvaccinum");
		model5.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
		

        JSONArray foreignKeys3 = new JSONArray();
        JSONObject key3 = new JSONObject();
        key3.put("selfKey", "archive_id");
        key3.put("foreClass", "platform_pet_archives");
        key3.put("foreKey", "id");
        key3.put("foreName", "name");
        foreignKeys2.add(key3);
        model5.put("foreignKey", foreignKeys3);
	    tables.add(model5);
	    */
        /*
        JSONObject model7 = new JSONObject();
        model7.put("tableName", "platform_user_browse");
        model7.put("shortName", "userbrowse");
        model7.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗

        JSONArray foreignKeys7 = new JSONArray();
        JSONObject key7 = new JSONObject();
        key7.put("selfKey", "group_id");
        key7.put("foreClass", "platform_group");
        key7.put("foreKey", "id");
        key7.put("foreName", "name");
        foreignKeys7.add(key7);

        JSONObject key8 = new JSONObject();
        key8.put("selfKey", "post_id");
        key8.put("foreClass", "platform_user_post");
        key8.put("foreKey", "id");
        key8.put("foreName", "name");
        foreignKeys7.add(key8);
        model7.put("foreignKey", foreignKeys7);
	    tables.add(model7);


		JSONObject model3 = new JSONObject();
		model3.put("tableName", "platform_user_comments");
		model3.put("shortName", "usercomments");
		model3.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗

        JSONArray foreignKeys2 = new JSONArray();
        JSONObject key2 = new JSONObject();
        key2.put("selfKey", "group_id");
        key2.put("foreClass", "platform_group");
        key2.put("foreKey", "id");
        key2.put("foreName", "name");
        foreignKeys2.add(key2);

        JSONObject key3 = new JSONObject();
        key3.put("selfKey", "post_id");
        key3.put("foreClass", "platform_user_post");
        key3.put("foreKey", "id");
        key3.put("foreName", "name");
        foreignKeys2.add(key3);
        model3.put("foreignKey", foreignKeys2);
	    tables.add(model3);

		JSONObject model2 = new JSONObject();
		model2.put("tableName", "platform_user_like");
		model2.put("shortName", "userlike");
		model2.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
	    

        JSONArray foreignKeys3 = new JSONArray();
        JSONObject key4 = new JSONObject();
        key4.put("selfKey", "group_id");
        key4.put("foreClass", "platform_group");
        key4.put("foreKey", "id");
        key4.put("foreName", "name");
        foreignKeys3.add(key4);

        JSONObject key5 = new JSONObject();
        key5.put("selfKey", "post_id");
        key5.put("foreClass", "platform_user_post");
        key5.put("foreKey", "id");
        key5.put("foreName", "name");
        foreignKeys3.add(key5);
        model2.put("foreignKey", foreignKeys3);
	    tables.add(model2);
		

		JSONObject model5 = new JSONObject();
		model5.put("tableName", "platform_user_post");
		model5.put("shortName", "userpost");
		model5.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model5);
		

        JSONArray foreignKeys1 = new JSONArray();
        JSONObject key6 = new JSONObject();
        key6.put("selfKey", "group_id");
        key6.put("foreClass", "platform_group");
        key6.put("foreKey", "id");
        key6.put("foreName", "name");
        foreignKeys1.add(key6);
        model5.put("foreignKey", foreignKeys1);
	    tables.add(model5);
	    */

        JSONObject model9 = new JSONObject();
        model9.put("tableName", "platform_product_outrecord");
        model9.put("shortName", "prooutrecord");
        model9.put("statusType", 0);//状态类型：0无，1是
        model9.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model9);
	    
        JSONObject model10 = new JSONObject();
        model10.put("tableName", "platform_product_inrecord");
        model10.put("shortName", "proinrecord");
        model10.put("statusType", 0);//状态类型：0无，1是
        model10.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
	    tables.add(model10);

        
        System.out.println(tables.toJSONString());
        
        //开始解析数据
        startGene(tables,dbName, url,user,password,pref,shortName); 
        System.out.println(geArray.toJSONString());
        int i = 0;
        for (Object object : geArray) {
        	JSONObject b = (JSONObject) object;
        	if(!b.containsKey("methods")) {
        		b.put("methods", new JsonArray());
            	geArray.set(i, b);
        	}
        	i++;
		}
        for (Object object : geArray) {
        	geneCodeStart(object);
		}
        
	}
	
	public static void main2(String[] args) {
		String dbName = "npm_bobahappy_co";
		String url = "jdbc:mysql://124.222.136.19:3306/"+dbName;  
        String user = "npm_bobahappy_co";  
        String password = "cyT5hrCEYb224YLY";  
        String pref = "eb_";  //表前缀
        String shortName = "company";  //表简写
		String tableName = "platform_company_info";
		
		geArray = new JSONArray();

        JSONArray tables = new JSONArray();
        /*
        1、配置表
        tableName： platform_company_info
        shortName： company
        foreignKey： 下面的外键
        formWay： 0 //表单方式，0表示BuildForm 1表示弹窗
        2、外键
        selfKey： parent_id
        foreClass： platform_company_info
        foreKey： id
        foreName： name
         
        */
        
        //配置表名
        JSONObject company = new JSONObject();
        company.put("tableName", "platform_company_info");
        company.put("shortName", "company");
        
        //外键配置
        JSONArray foreignKeys = new JSONArray();
        JSONObject key = new JSONObject();
        key.put("selfKey", "parent_id");
        key.put("foreClass", "platform_company_info");
        key.put("foreKey", "id");
        key.put("foreName", "name");
        foreignKeys.add(key);
        company.put("foreignKey", foreignKeys);
        company.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
        
        
        
        //配置表名
        JSONObject contract = new JSONObject();
        contract.put("tableName", "platform_contract_info");
        contract.put("shortName", "contract");
        
        //外键配置
        JSONArray foreignKeys2 = new JSONArray();
        JSONObject key2 = new JSONObject();
        key2.put("selfKey", "company_id");
        key2.put("foreClass", "platform_company_info");
        key2.put("foreKey", "id");
        key2.put("foreName", "name");
        foreignKeys2.add(key2);
        
        JSONObject key3 = new JSONObject();
        key3.put("selfKey", "city_id");
        key3.put("foreClass", "platform_city");
        key3.put("foreKey", "id");
        key3.put("foreName", "name");
        foreignKeys2.add(key3);
        
        contract.put("foreignKey", foreignKeys2);
        contract.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
        
        //配置表名
        JSONObject city = new JSONObject();
        city.put("tableName", "platform_city");
        city.put("shortName", "city");
        
        //外键配置
//        JSONArray foreignKeys3 = new JSONArray();
//        JSONObject key3 = new JSONObject();
//        key3.put("selfKey", "company_id");
//        key3.put("foreClass", "platform_company_info");
//        key3.put("foreKey", "id");
//        key3.put("foreName", "name");
//        foreignKeys2.add(key3);
//        city.put("foreignKey", foreignKeys3);
        city.put("formWay", 0);//表单方式，0表示BuildForm 1表示弹窗
        
        

        //配置表名
        JSONObject project = new JSONObject();
        project.put("tableName", "platform_project_info");
        project.put("shortName", "project");
        
        //外键配置
        JSONArray foreignKeys5 = new JSONArray();
        JSONObject key5 = new JSONObject();
        key5.put("selfKey", "city_id");
        key5.put("foreClass", "platform_city");
        key5.put("foreKey", "id");
        key5.put("foreName", "code");
        foreignKeys5.add(key5);
        project.put("foreignKey", foreignKeys5);
        project.put("formWay", 1);//表单方式，0表示BuildForm 1表示弹窗
        
        tables.add(company);
        tables.add(contract);
        tables.add(city);
        tables.add(project);
        
        System.out.println(tables.toJSONString());
        
        //开始解析数据
        startGene(tables,dbName, url,user,password,pref,shortName); 
        
        int i = 0;
        for (Object object : geArray) {
        	JSONObject b = (JSONObject) object;
        	if(b.containsKey("methods")) {
        		System.out.println("没有thods");
        		b.put("methods", new JsonArray());
            	geArray.set(i, b);
        	}else {
        		System.out.println("有thods");
        	}
        	i++;
		}
        System.out.println(geArray.toJSONString());
        
        for (Object object : geArray) {
        	geneCodeStart(object);
		}

        System.out.println("代码生成结束");
	}
	
	private static void  geneCodeStart(Object object) {
		try {
			JSONObject root = (JSONObject) object;
			System.out.println("开始生成代码....【"+root.getString("tableName")+"】");
			String tableClass =  toCamelCase(root.getString("tableName")); 
			 

			Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);  
			cfg.setClassForTemplateLoading(Test2.class, "/templates/"); 

			cfg.setDefaultEncoding("UTF-8");  
			cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);  
			cfg.setLogTemplateExceptions(false);  
			cfg.setWrapUncheckedExceptions(true); 
			
			String tempModelName = "model.ftl";
			geneCode(root.getString("shortName"), tableClass, root, cfg, tempModelName);
			
			String tempDaoName = "dao.ftl";
			geneCode(root.getString("shortName"), tableClass+"Dao", root, cfg, tempDaoName);
			
			String tempServiceName = "service.ftl";
			geneCode(root.getString("shortName"), tableClass+"Services", root, cfg, tempServiceName);
			
			String tempControllerName = "controller.ftl";
			geneCode(root.getString("shortName"), tableClass+"Controller", root, cfg, tempControllerName);
			
			String tempRouteName = "route.ftl";
			geneCode(root.getString("shortName"), root.getString("shortName"), root, cfg, tempRouteName);
			
			String tempApiName = "api.ftl";
			geneCodeJs(root.getString("shortName"), root, cfg, tempApiName);
			
			if((Integer)root.get("formWay") == 0) {
				String tempIndexName = "front2/index.ftl";
				geneCodeVue(root.getString("shortName"), "index", root, cfg, tempIndexName);
			}else {

				String tempIndexName = "front/index.ftl";
				geneCodeVue(root.getString("shortName"), "index", root, cfg, tempIndexName);
				String tempFormName = "front/form.ftl";
				geneCodeVue(root.getString("shortName")+"/components", "menusForm", root, cfg, tempFormName);
			}
		} catch (TemplateNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void startGene(JSONArray tables,String dbName,String url,String user,String password,String pref,String shortName) {
        try (Connection conn = DriverManager.getConnection(url, user, password)) {  
            DatabaseMetaData metaData = conn.getMetaData();  
            String tableComment = "";
            
            //第一步，把所有要生成的表遍历完放到一个数组中
            for (Object object : tables) {
    			JSONObject table = (JSONObject) object;
    			JSONObject tableInfo = getTableInfos(dbName, table.getString("tableName"), pref, conn, metaData, tableComment,shortName);
    			tableInfo.put("tableName", table.getString("tableName"));
    			tableInfo.put("shortName", table.getString("shortName"));
    			tableInfo.put("tableClass", toCamelCase(table.getString("tableName")));
    			
    			tableInfo.put("formWay", table.getInteger("formWay"));
    			tableInfo.put("statusType", table.getInteger("statusType"));
    			tableInfo.put("foreignKey", table.getJSONArray("foreignKey"));
    			

    			JSONArray methods = table.getJSONArray("foreignKey");
    			if(null != methods) {
        			JSONArray selfMethods = new JSONArray();
    				//给自己查询model添加关联方法
    				for (Object keys : methods) {
    					JSONObject key = (JSONObject) keys;

    					JSONObject method =new JSONObject();
    					method.put("methodName", toCamelCase2(key.getString("selfKey")));
    					method.put("foreignClass", toCamelCase(key.getString("foreClass")));
    					method.put("selfKey", key.getString("selfKey"));
    					method.put("foreKey", key.getString("foreKey"));
    					method.put("foreName", key.getString("foreName"));
    					method.put("selfName", key.getString("selfKey")+"_"+key.getString("foreName"));
    					selfMethods.add(method);
    				}
        			tableInfo.put("selfMethods", selfMethods);
    			}else {
        			tableInfo.put("selfMethods", new JSONArray());
    			}
				geArray.add(tableInfo);
    			
    		}// end for
            //第二步，判断是否有外键，如果有外键，给生成配置中添加方法
            for (Object object : tables) {
    			JSONObject table = (JSONObject) object;
    			if(null != table.get("foreignKey")) {
    				//给外键类添加方法
    				JSONArray forekeys = (JSONArray) table.get("foreignKey");
    				for (Object keys : forekeys) {
    					JSONObject key = (JSONObject) keys;
    					
    					String methodNameString = "getCateBy"+capitalizeFirstLetter(key.getString("foreKey"))+capitalizeFirstLetter(key.getString("foreName"));
    					//找到table配置
    					int i = 0;
    					for (Object ta : geArray) {
    						JSONObject taSetting = (JSONObject) ta;
    						if(taSetting.getString("tableName").equalsIgnoreCase(key.getString("foreClass"))) {

    							JSONArray methods = taSetting.getJSONArray("methods");
    							if(null == methods)methods = new JSONArray();
    							
    							boolean isExists = false;
    							for (Object me : methods) {
	    							JSONObject meth = (JSONObject) me;
	    							if(meth.getString("methodName").equalsIgnoreCase(methodNameString)) {
	    								isExists = true;
	    							}
    							}
    							//添加方法
    							if(!isExists) {
    								JSONObject newMethod = new JSONObject();
    								newMethod.put("methodName", methodNameString);
    								newMethod.put("properties", key.getString("foreKey")+","+key.getString("foreName"));
    								methods.add(newMethod);
    								taSetting.put("methods", methods);
    								geArray.set(i, taSetting);
    							}
    							
    							break;
    						}
    						i++;
    					}
    					
    					
    					
    				}//end for
    				
    			}else{
    				table.put("foreignKey", new JSONArray() );
    			}// end if
    			
    		}// end for
            
            
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
		
		
	}
	
	
	//解析数据库表
		private static JSONObject getTableInfos(String dbName, String tableName, String pref, Connection conn,
				DatabaseMetaData metaData, String tableComment,String shortName) throws SQLException {
			JSONObject table = new JSONObject();
			//获取表信息 
			try (Statement stmt = conn.createStatement();  
			     ResultSet rs = stmt.executeQuery(  
			         "SELECT TABLE_COMMENT FROM information_schema.TABLES " +  
			         "WHERE TABLE_SCHEMA = '"+dbName+"' AND TABLE_NAME = '"+pref + tableName + "'"  
			 )) {  
			    if (rs.next()) {  
			    	tableComment = rs.getString("TABLE_COMMENT");  
			        System.out.println("Table Comment: " + tableComment);  
			        System.out.println("-------------------");  
			        table.put("tableComment", tableComment);
			    } else {  
			        System.out.println("No table comment found for " + pref +tableName);  
			    }  
			}  
			
			//获取表字段信息
			JSONArray columns = new JSONArray();
			try (ResultSet rs = metaData.getColumns(null, null, pref +tableName, null)) {  
			    while (rs.next()) {  
			        String columnName = rs.getString("COLUMN_NAME");  
			        String dataType = rs.getString("TYPE_NAME");  
			        int dataSize = rs.getInt("COLUMN_SIZE");  
			        boolean isNullable = rs.getBoolean("NULLABLE");  
			        String comment = rs.getString("REMARKS");  

			        if(columnName.equalsIgnoreCase("is_del")) {
			        	continue;
			        }
			        if(columnName.equalsIgnoreCase("created_time")) {
			        	continue;
			        }
			        if(columnName.equalsIgnoreCase("created_uid")) {
			        	continue;
			        }
			        if(columnName.equalsIgnoreCase("updated_time")) {
			        	continue;
			        }
			        if(columnName.equalsIgnoreCase("updated_uid")) {
			        	continue;
			        }
			        JSONObject column = new JSONObject();
			        column.put("columnName", columnName);
			        column.put("dataType", dataType);
			        column.put("comment", comment);
			        column.put("dataSize", dataSize);
			        column.put("isNull", isNullable);
			        columns.add(column);
			    }//end while
		        table.put("tableProperties", columns);
			} catch (Exception e) {
				System.out.println(e);
			}
			return table;
		}
	/*
	//解析数据库表
	private static void alalyis(String dbName, String tableName, String pref, Connection conn,
			DatabaseMetaData metaData, String tableComment,String shortName) throws SQLException {
		//获取表信息 
		try (Statement stmt = conn.createStatement();  
		     ResultSet rs = stmt.executeQuery(  
		         "SELECT TABLE_COMMENT FROM information_schema.TABLES " +  
		         "WHERE TABLE_SCHEMA = '"+dbName+"' AND TABLE_NAME = '"+pref + tableName + "'"  
		 )) {  
		    if (rs.next()) {  
		    	tableComment = rs.getString("TABLE_COMMENT");  
		        System.out.println("Table Comment: " + tableComment);  
		        System.out.println("-------------------");  
		    } else {  
		        System.out.println("No table comment found for " + pref +tableName);  
		    }  
		}  
		
		//获取表字段信息
		JSONArray columns = new JSONArray();
		try (ResultSet rs = metaData.getColumns(null, null, pref +tableName, null)) {  
		    while (rs.next()) {  
		        String columnName = rs.getString("COLUMN_NAME");  
		        String dataType = rs.getString("TYPE_NAME");  
		        int dataSize = rs.getInt("COLUMN_SIZE");  
		        boolean isNullable = rs.getBoolean("NULLABLE");  
		        String comment = rs.getString("REMARKS");  

		        if(columnName.equalsIgnoreCase("is_del")) {
		        	continue;
		        }
		        if(columnName.equalsIgnoreCase("created_time")) {
		        	continue;
		        }
		        if(columnName.equalsIgnoreCase("created_uid")) {
		        	continue;
		        }
		        if(columnName.equalsIgnoreCase("updated_time")) {
		        	continue;
		        }
		        if(columnName.equalsIgnoreCase("updated_uid")) {
		        	continue;
		        }
		        JSONObject column = new JSONObject();
		        column.put("columnName", columnName);
		        column.put("dataType", dataType);
		        column.put("comment", comment);
		        column.put("dataSize", dataSize);
		        column.put("isNull", isNullable);
		        columns.add(column);
		    }//end while
		    
		    for (Object object : columns) {
		    	JSONObject column = (JSONObject) object;
		    	if(column != null) {
		    		System.out.println(column.toJSONString());
		            System.out.println("-------------------");  
		    	}
			}
		    
		    
		    //开始生成代码
		    System.out.println("开始生成代码....");
		    String tableClass =  toCamelCase(tableName); 
		    
			Map<String, Object> root = new HashMap<>();  
			root.put("tableComment", tableComment);  
			root.put("tableClass", tableClass);  
			root.put("tableName", tableName);   
			root.put("columns", columns);     
			root.put("shortName", shortName);  

			Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);  
			cfg.setClassForTemplateLoading(Test2.class, "/templates/"); 

			cfg.setDefaultEncoding("UTF-8");  
			cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);  
			cfg.setLogTemplateExceptions(false);  
			cfg.setWrapUncheckedExceptions(true); 
			
			String tempModelName = "model.ftl";
			geneCode(tableClass, root, cfg, tempModelName);
			
			String tempDaoName = "dao.ftl";
			geneCode(tableClass+"Dao", root, cfg, tempDaoName);
			
			String tempServiceName = "service.ftl";
			geneCode(tableClass+"Services", root, cfg, tempServiceName);
			
			String tempControllerName = "controller.ftl";
			geneCode(tableClass+"Controller", root, cfg, tempControllerName);
			
			String tempRouteName = "route.ftl";
			geneCode(shortName, root, cfg, tempRouteName);
			
			String tempApiName = "api.ftl";
			geneCodeJs(shortName, root, cfg, tempApiName);
			

			String tempIndexName = "front/index.ftl";
			geneCodeVue("index", root, cfg, tempIndexName);
			String tempFormName = "front/form.ftl";
			geneCodeVue("menusForm", root, cfg, tempFormName);
		    
		    //end 
		    System.out.println("结束生成代码....");
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	*/

	/**
	 * 使用模板生成代码
	 * */
	private static void geneCode(String dirName,String tableClass, Map<String, Object> root, Configuration cfg, String tempName)
			throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException,
			UnsupportedEncodingException, FileNotFoundException, TemplateException {
		File file = new File("/Users/simple/Documents/projects/code/"+dirName);
		if(!file.exists()) {
			file.mkdirs();
		}
		Template temp = cfg.getTemplate(tempName);
		Writer out = new OutputStreamWriter(new FileOutputStream("/Users/simple/Documents/projects/code/"+dirName+"/"+tableClass+".php"), "UTF-8");
		temp.process(root, out);
	}
	
	/**
	 * 使用模板生成代码
	 * */
	private static void geneCodeJs(String tableClass, Map<String, Object> root, Configuration cfg, String tempName)
			throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException,
			UnsupportedEncodingException, FileNotFoundException, TemplateException {
		File file = new File("/Users/simple/Documents/projects/code/"+tableClass);
		if(!file.exists()) {
			file.mkdirs();
		}
		Template temp = cfg.getTemplate(tempName);    
		
		Writer out = new OutputStreamWriter(new FileOutputStream("/Users/simple/Documents/projects/code/"+tableClass+"/"+tableClass+".js"), "UTF-8");
		temp.process(root, out);
	}
	
	/**
	 * 使用模板生成代码
	 * */
	private static void geneCodeVue(String dirName,String tableClass, Map<String, Object> root, Configuration cfg, String tempName)
			throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException,
			UnsupportedEncodingException, FileNotFoundException, TemplateException {
		File file = new File("/Users/simple/Documents/projects/code/"+dirName);
		if(!file.exists()) {
			file.mkdirs();
		}
		Template temp = cfg.getTemplate(tempName);    
		Writer out = new OutputStreamWriter(new FileOutputStream("/Users/simple/Documents/projects/code/"+dirName+"/"+tableClass+".vue"), "UTF-8");
		temp.process(root, out);
	}
	

	/**
	 * 使用模板生成代码
	 * */
	public static String toCamelCase(String s) {  
	    if (s == null) {  
	        return null;  
	    }  
	      
	    s = s.toLowerCase();  
	      
	    StringBuilder sb = new StringBuilder();  
	    boolean upperCase = true;  
	    for (int i = 0; i < s.length(); i++) {  
	        char c = s.charAt(i);  
	          
	        if (c == '_') {  
	            upperCase = true;  
	        } else if (upperCase) {  
	            sb.append(Character.toUpperCase(c));  
	            upperCase = false;  
	        } else {  
	            sb.append(c);  
	        }  
	    }  
	      
	    return sb.toString();  
	}
	
	/**
	 * 使用模板生成代码
	 * */
	public static String toCamelCase2(String s) {  
	    if (s == null) {  
	        return null;  
	    }  
	      
	    s = s.toLowerCase();  
	      
	    StringBuilder sb = new StringBuilder();  
	    boolean upperCase = true;  
	    for (int i = 0; i < s.length(); i++) {  
	        char c = s.charAt(i);  
	          
	        if (c == '_') {  
	            upperCase = true;  
	        } else if (upperCase) {  
	        	if(i==0) {
		            sb.append(Character.toLowerCase(c));  
	        	}else {
		            sb.append(Character.toUpperCase(c));  
	        	}
	            upperCase = false;  
	        } else {  
	            sb.append(c);  
	        }  
	    }  
	      
	    return sb.toString();  
	}
	
	/**
	 * 首字母转大写
	 * */
	public static String capitalizeFirstLetter(String input) {
        boolean capitalizeNext = true;
        StringBuilder output = new StringBuilder();
        for (char c : input.toCharArray()) {
            if (capitalizeNext) {
                output.append(Character.toUpperCase(c));
                capitalizeNext = false;
            } else if (c == ' ') {
                output.append(c);
                capitalizeNext = true;
            } else {
                output.append(Character.toLowerCase(c));
            }
        }
        return output.toString();
    }

}
